package com.nfit.shopcommon.utils;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:HhreadLocalUtil
 * Package:com.nfit.shopcommon.utils
 * Description
 *
 * 用于存储登入用户,存在线程中
 *
 * @Author L
 * @Create 2024/11/15 18:56
 */
@Schema(description = "单线线程工具类,主要用于存储于登入用户数据")
public class ThreadLocalUtil {
    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = ThreadLocal.withInitial(HashMap::new);

    // 存储数据
    public static <T> void set(String key, T value) {
        THREAD_LOCAL.get().put(key, value);
    }

    // 获取数据
    public static <T> T get(String key) {
        return (T) THREAD_LOCAL.get().get(key);
    }

    // 移除数据
    public static void remove(String key) {
        THREAD_LOCAL.get().remove(key);
    }

    // 清除所有数据
    public static void removeAll() {
        THREAD_LOCAL.remove();
    }
}

